// pages/order/order.js
const myRequest = require('../../assets/js/request.js');
const api = require('../../assets/js/api.js')
let arrs = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderlists: [],
    num: [],
    price: [],
    orderinfo: [{
        text: '全部',
        pay: 2
      },
      {
        text: '待付款',
        pay: 0
      },
      {
        text: '已付款',
        pay: 1
      }, {
        text: '待收货',
        pay: 1
      }, {
        text: '已完成',
        pay: 3
      }
    ],
    activeIndex: 0,
    ordertype: 2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.showData()
  },
  showData: function() {
    let that = this
    let arr = []
    let numarr = []
    let pricearr = []
    console.log(111)
    wx.getStorage({
      key: 'orderlists',
      success: function(res) {
        console.log(res.data)
        if (that.data.ordertype == 2) {
          that.setData({
            orderlists: res.data
          })
        } else if (that.data.ordertype == 3) {
          that.setData({
            orderlists: []
          })
        } else {
          res.data.forEach((item, index) => {
            if (item[0].payinfo == that.data.ordertype) {
              arr.push(item)
            }
          })
          that.setData({
            orderlists: arr
          })
        }
        that.data.orderlists.forEach((item, index) => {
          let a = 0,
            b = 0
          for (let i = 0; i < item.length; i++) {
            a = a + item[i].num
            b = b + item[i].num * item[i].price
          }
          numarr.push(a)
          pricearr.push(b.toFixed(2))
        })
        console.log(numarr)
        console.log(pricearr)
        that.setData({
          num: numarr,
          price: pricearr
        })
      },
    })
  },

  //选择订单类别
  chooseorder: function(e) {
    console.log(e)
    let that = this
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
      ordertype: e.currentTarget.dataset.payinfo
    })
    this.showData()
  },

  //没有订单时，点击回到首页
  goshopping: function() {
    wx.switchTab({
      url: '../../pages/home/home',
    })
  },

  //订单详情页，点击首页图标回到首页
  backhome: function() {
    wx.switchTab({
      url: '../../pages/home/home',
    })
  },

  //点击购物车图标，继续购买商品
  goonbuy: function(e) {
    let myid = e.currentTarget.dataset.id
    console.log(myid)
    myRequest._get(api.goodsList + '?id=' + myid).then((res) => {
      console.log(res.data)
      wx.setStorage({
        key: 'detailItem',
        data: res.data.goodslist,
      })
      wx.navigateTo({
        url: '/pages/detail/detail',
      })
    })
  },
  //继续支付
  continuepay:function(e){
    console.log(e)
    let that=this
    wx.showModal({
      title: '继续支付',
      content: '请认真核对订单信息',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.getStorage({
            key: 'orderlists',
            success: function(res) {
              console.log(111)
              res.data[e.currentTarget.dataset.index][0].payinfo=1
              wx.setStorage({
                key: 'orderlists',
                data: res.data,
              })
              that.showData()
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 2000
              })
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.showData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})